#include"overlay.h"
#include "dlfcn.h"

#include <stdio.h>
#include <stdlib.h>
#include <errno.h>

#define VARNAME "VGM_INSTALL_PATH"
#include "osdependent_func.h"

int main(int argc, char** argv)
{

	struct GeneratorCtx* ctx = NULL;
	const char* test_str = "Follow the link"
	 "http://bamnosi.com \n\nThis is a long string meant to display in a"
	 "in One Frame. Some lines are very long , and then again there are paragraph"
	 "breaks.\n\nThe text widget shall wrap words, but be neither too big nor too"
	 "small, and have a pleasing aspect ratio (say, in pixels, 4:3 or 16:9).";

	char cCurrentPath[PATH_LEN];
	int ret = 0;

	if (!GetCurrentDir(cCurrentPath, sizeof(cCurrentPath)))
	{
		return -1;
	}

	ret = of_setenv(VARNAME,cCurrentPath,0);
	if(ret != 0)
	{
		printf("setenv failed(%d)\n",errno);
		return -1;
	}


#ifndef CHECK_MEMORY_LEAK
	void *overlay_lib = dlopen("./libOverlay.dll", RTLD_LAZY);
	if (overlay_lib == NULL)
	{
		perror("unable to open the overlay lib");
		return -1;
	}

	init_generator_t init_generator = (init_generator_t) dlsym(overlay_lib,
			"init_generator");
	if (init_generator == NULL)
	{
		printf("there is no function named init_generator\n");
	}
	dinit_generator_t dinit_generator = (dinit_generator_t) dlsym(overlay_lib,
			"dinit_generator");
	if (dinit_generator == NULL)
	{
		printf("there is no function named dinit_generator\n");
	}

	add_phase_t add_phase = (add_phase_t) dlsym(overlay_lib, "add_phase");
	if (add_phase == NULL)
	{
		printf("there is no function named add_phase\n");
	}

	set_audio_t set_audio = (set_audio_t) dlsym(overlay_lib, "set_audio");
	if (set_audio == NULL)
	{
		printf("there is no function named set_audio\n");
	}

#endif
	ctx = init_generator("test.mp4");
	if (!ctx)
	{
		std::cerr << "unable to initialize generator\n";
		return -1;
	}

	ret = set_audio(ctx, "test//audio//Shattere-Marcomed-10379_hifi.wav");
	if(ret < 0)
	{
		std::cerr << "unable to set audio\n";
		dinit_generator(ctx);
		return -1;
	}

/*
	ret =
			add_phase(ctx,
					"test//preroll",
					test_str, 1, 3, 5, 0xffffff, //text color
					-1, // text x pos
					-1, //text y pos
					0x000000, //shadow color
					3, 3);
	if (ret < 0)
	{
		printf("failed to add phase %d\n", ret);
		return -1;
	}
*/

	ret =
			add_phase(ctx,
					"test//main1",
					test_str, 800, 
					3, 3, //min max time
					0x000000, //text color
					-1, // text x pos
					-1, //text y pos
					0xffffff, //shadow color
					3, 3);
	if (ret < 0)
	{
		printf("failed to add phase %d\n", ret);
		return -1;
	}

	dinit_generator(ctx);

#ifndef CHECK_MEMORY_LEAK
	dlclose(overlay_lib);
#endif


	return 0;
}
